component{
	include "../../inc/lib.cfm"; 
	//发件箱
	public any function getSendBoxList(rc){
		var result = {};
		var offset = (rc.page-1)*rc.rows;
		hql = "from sms where " & rc.sqlWhere & rc.sqlSort;
		var result["rows"] = ormExecuteQuery(hql, false, {maxResults=rc.rows, offset=offset});
		var totalhql = "select count(id) as total from sms where " & rc.sqlWhere ;
		var result["total"] = ormExecuteQuery(totalhql, true);
		
		return result;
	}
	
	//收件箱
	public any function getInBoxList(rc){
		var result = {};
		var offset = (rc.page-1)*rc.rows;
		hql = "from sms s left join s.smslog as l where " & rc.sqlWhere & rc.sqlSort;
		var result["rows"] = ormExecuteQuery(hql, false, {maxResults=rc.rows, offset=offset});
		var totalhql = "select count(s.id) as total from sms s left join s.smslog as l where " & rc.sqlWhere ;
		var result["total"] = ormExecuteQuery(totalhql, true);
		
		/*
			通过left join将两个实体类联合起来返回的结果形式是
			result rows为一个结构，rows对象是一个数组，而该数组的结构对象每个包含两个component对象，一个是sms，一个是smslog
					[1]
						[1]是sms
						[2]是smslog
					[2]
						[1]是sms
						[2]是smslog
					[3]
						[1]是sms
						[2]是smslog
					...
			我们通过遍历重新组成新的json结构对象进行输出				
		*/
		
		var newResult = [];
		for(var i = 1; i <= arraylen(result.rows); i++)
		{
			var tmpResult = {};
			var o = result.rows[i][1];;
			tmpResult["id"] = o.getId();
			tmpResult["subject"] = o.getSubject();
			tmpResult["created"] = o.getCreated();
						
			var smslog = result.rows[i][1].getSmsLog();
			for(var j = 1; j<= arraylen(smslog); j++){
				if( smslog[j].getToUserId() == session.auth.userid ){
					
					var u = entityLoadByPk("user", smslog[j].getFromUserID());
					tmpResult['readed'] = smslog[j].getReaded();
					tmpResult['fromusername'] = u.getName();
				}
			}
			newResult[i] = tmpResult;
		}
		result["rows"] = newResult;
		return result;
	}

	//新增操作
	public any function save(any rc){
		var lstIds = '';
		if(rc.alluser == 0){
			var resultOrg = {};
			var hqlOrg = "from userorganization where organizationid in (#rc.organizationids#)";
			var resultOrg = ormExecuteQuery(hqlOrg,false);
	
			var arrOrgIds = arrayNew();
			for(i = 1; i <= arraylen(resultOrg); i++)
			{
				arrayappend(arrOrgIds,resultOrg[i].getId());
			}		
			
			var resultRole = {};
			var hqlRole = "from userrole where roleid in (#rc.roleids#)";
			var resultRole = ormExecuteQuery(hqlRole,false);
			
			var arrRoleIds = arrayNew();
			for(i = 1; i <= arraylen(resultRole); i++)
			{
				arrayappend(arrRoleIds,resultRole[i].getId());
			}		
			
			var arrUserIds = listtoarray(rc.userids);
			var arrResult = arraymerge(arraymerge(arrUserIds,arrOrgIds),arrRoleIds);
			lstIds = ListDeleteDuplicatesNoCase( arraytolist(arrResult) );
			if( listfindnocase(lstIds,0) > 0){
				lstIds = listdeleteat(lstIds, listfindnocase(lstIds,0) );
			}
		}else{
			var hqlUser = "from user";
			var resultUser = ormExecuteQuery(hqlUser,false);
			
			var arrUserIds = arrayNew();
			for(i = 1; i <= arraylen(resultUser); i++)
			{
				arrayappend(arrUserIds,resultUser[i].getId());
			}
			
			lstIds = arraytolist(arrUserIds);
			
		}
		
		//下面是将自己id从lstIds中删除
		lstIds = ListRemoveByString (lstIds,session.auth.userid);
		
		transaction {
			var q = entityNew("sms");
	        q.setContent(rc.content);
	        q.setsubject(rc.subject);
	        q.setfromuserid(session.auth.userid);
	        entitySave(q);
		}

		//循环发送给相应人员
		for(var i=1; i<=listlen(lstIds); i++){
			transaction {
	            var s = entityNew("smslog");
				s.setToUserId(listgetat(lstIds,i));
	            s.setFromUserId(session.auth.userid);
				q.addSmslog(s);
				entitySave(s);
			}
		}
		
		transactionCommit();
		
		return true;
	}
	
	//回复操作
	public any function replysms(any rc){
		result = entityLoadByPk("sms", rc.smsid);
		
		transaction {
            var q = entityNew("sms");
            q.setContent(rc.content);
            q.setsubject(rc.subject);
            q.setfromuserid(session.auth.userid);
            entitySave(q);
            
            var s = entityNew("smslog");
            s.setToUserId( result.getFromuserid() );
            s.setFromUserId(session.auth.userid);
			q.addSmslog(s);
			entitySave(s);
            
			transactionCommit();
		}
		return q;
	}
	
	
	public any function get(rc){
		result = entityLoadByPk("sms",rc.id);
		
		var hql = 'from smslog where smsid = #result.getid()# and touserid = #session.auth.userid#';
		var smslog = ormExecuteQuery(hql, true);
		
		//如果是发件箱查看消息，则不需要去smslog查询
		if(!isnull(smslog)){
			smslog.setreaded(now());
			entitySave(smslog);
			transactionCommit();
		}
		
		return result;
	}
	
	//收件箱删除
	public any function deleteinbox(numeric id){
		transaction {
            var s = entityLoadByPk("sms", arguments.id);
           	var hql = 'delete from smslog where smsid = #s.getId()# and touserid = #session.auth.userid#';
			var smslog = ormExecuteQuery(hql, true);
        }
	}
	
	//发件箱删除，删除smslog的所有相关内容
	public any function deletesendbox(numeric id){
		transaction {
            var s = entityLoadByPk("sms", arguments.id);
           	var hql = 'delete from smslog where smsid  = #s.getId()#';
			var smslog = ormExecuteQuery(hql, true);
           	entityDelete(s);
            transactionCommit();
        }
	}
	
}